package com.ruoyi.develop.lvDo.controller;

import java.math.BigDecimal;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.uuid.IdUtils;
import com.ruoyi.constants.BusinessConstants;
import com.ruoyi.develop.goodsOrderIn.domain.GoodsOrderIn;
import com.ruoyi.develop.goodsOrderIn.service.IGoodsOrderInService;
import com.ruoyi.develop.goodsSale.domain.GoodsSale;
import com.ruoyi.develop.goodsSale.service.IGoodsSaleService;
import com.ruoyi.develop.goodsSaleMaterial.service.IGoodsSaleMaterialService;
import com.ruoyi.develop.goodsSalebuy.domain.GoodsSalebuy;
import com.ruoyi.develop.goodsSalebuy.service.IGoodsSalebuyService;
import com.ruoyi.develop.goodsSalebuyall.domain.GoodsSalebuyall;
import com.ruoyi.develop.goodsSalebuyall.service.IGoodsSalebuyallService;
import com.ruoyi.develop.goodsSaledo.domain.GoodsSaledo;
import com.ruoyi.develop.goodsSaledo.service.IGoodsSaledoService;
import com.ruoyi.develop.lvBuy.domain.LvBuy;
import com.ruoyi.develop.lvBuy.service.ILvBuyService;
import com.ruoyi.develop.orderIronRecord.domain.OrderIronRecord;
import com.ruoyi.develop.orderIronRecord.service.IOrderIronRecordService;
import com.ruoyi.develop.process.processPleaseBuy.service.IProcessPleaseBuyService;
import com.ruoyi.develop.projectBaseInfo.domain.ProjectBaseInfo;
import com.ruoyi.develop.projectBaseInfo.service.IProjectBaseInfoService;
import org.springframework.beans.BeanUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.develop.lvDo.domain.LvDo;
import com.ruoyi.develop.lvDo.service.ILvDoService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 请购单Controller
 *
 * @author qh
 * @date 2024-08-09
 */
@RestController
@RequestMapping("/lvDo/lvDo")
public class LvDoController extends BaseController {
    @Autowired
    private ILvDoService lvDoService;
    @Autowired
    private IGoodsSaledoService goodsSaledoService;
    @Autowired
    private IGoodsSaleService goodsSaleService;
    @Autowired
    private IProjectBaseInfoService projectBaseInfoService;
    @Autowired
    private IGoodsOrderInService goodsOrderInService;
    @Autowired
    private IGoodsSaleMaterialService iGoodsSaleMaterialService;
    @Autowired
    private IOrderIronRecordService orderIronRecordService;
    @Autowired
    private IProcessPleaseBuyService processPleaseBuyService;
    @Autowired
    private ILvBuyService lvBuyService;
    @Autowired
    private IGoodsSalebuyallService goodsSalebuyallService;
    @Autowired
    private IGoodsSalebuyService goodsSalebuyService;




    /**
     * 查询请购单列表
     */
    @PreAuthorize("@ss.hasPermi('develop:LvDo:query')")
    @GetMapping("/list")
    public TableDataInfo list(LvDo entity) {
        startPage();
        List<LvDo> list = lvDoService.selectList(entity);
        return getDataTable(list);
    }

    @PreAuthorize("@ss.hasPermi('develop:LvDo:query')")
    @GetMapping("/listBySaleId")
    public TableDataInfo listBySaleId(LvDo entity) {
        startPage();
        List<LvDo> list = lvDoService.selectList(entity);
        return getDataTable(list);
    }

    @PreAuthorize("@ss.hasPermi('develop:LvDo:query')")
    @GetMapping("/listHaveBuy")
    public TableDataInfo listHaveBuy(LvDo entity) {
        startPage();
        List<LvDo> list = lvDoService.listHaveBuy(entity);
        return getDataTable(list);
    }

    /**
     * 导出请购单列表
     */
    @PreAuthorize("@ss.hasPermi('develop:LvDo:export')")
    @Log(title = "请购单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, LvDo entity) {
        List<LvDo> list = lvDoService.selectList(entity);
        ExcelUtil<LvDo> util = new ExcelUtil<LvDo>(LvDo.class);
        util.exportExcel(response, list, "请购单数据");
    }

    /**
     * 获取请购单详细信息
     */
    @PreAuthorize("@ss.hasPermi('develop:LvDo:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        return AjaxResult.success(lvDoService.selectById(id));
    }

    /**
     * 新增请购单
     */
    @PreAuthorize("@ss.hasPermi('develop:LvDo:add')")
    @Log(title = "请购单", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@RequestBody LvDo entity) {
        return toAjax(lvDoService.insert(entity));
    }

    /**
     * 修改请购单
     */
    @PreAuthorize("@ss.hasPermi('develop:LvDo:edit')")
    @Log(title = "请购单", businessType = BusinessType.UPDATE)
    @PutMapping("/edit")
    public AjaxResult edit(@RequestBody LvDo entity) {
        return toAjax(lvDoService.update(entity));
    }

    //采购弃审
    @PutMapping("/editReject")
    public AjaxResult editReject(@RequestBody LvDo entity) {
        LvDo lvDo = lvDoService.selectById(entity.getId());
        String changeStatus = entity.getStatus();
        if (StringUtils.isBlank(changeStatus)) {
            return AjaxResult.error("状态不能为空");
        }
        goodsSaledoService.updateStatusByMainId(BusinessConstants.GOODS_33, entity.getId());
//        if( BusinessConstants.PLEASE_BUY_SOURCE_MATERIAL.equals(lvDo.getSource())){
//            // 如果是材料采购 铁件记录
//            OrderIronRecord orderIronRecord  = orderIronRecordService.selectById(lvDo.getMainId());
//            orderIronRecord.setStatus(BusinessConstants.ORDER_IRON_STATE_REJECT);
//            orderIronRecordService.update(orderIronRecord);
//        }
        // 如果是库管驳回 更新订单产品状态
        if (BusinessConstants.PLEASE_BUY_STATUS_TECH_REJECT.equals(changeStatus)) {
            GoodsSaledo goodsSaledo = new GoodsSaledo();
            goodsSaledo.setMainId(entity.getId());
            List<GoodsSaledo> goodsSaledoList = goodsSaledoService.selectList(goodsSaledo);
            if (BusinessConstants.PLEASE_BUY_SOURCE_ORDER_TWICE.equals(lvDo.getSource())) {
                // 如果是二次采购 不需要更新订单状态 返回说定库存  更改请购单状态
//                GoodsOrderIn goodsOrderIn = goodsOrderInService.selectById(goodsSaledoItem.getGoodsId());
//                if (goodsOrderIn != null) {
//                    goodsOrderIn.setRemainderCount(goodsOrderIn.getRemainderCount().add(goodsSale.getLockCount()));
//                    goodsOrderInService.update(goodsOrderIn);
//                }

            } else {
                for (GoodsSaledo goodsSaledoItem : goodsSaledoList) {
                    goodsSaledoItem.setStatus(BusinessConstants.GOODS_33);
                    GoodsSale goodsSale = goodsSaleService.selectById(goodsSaledoItem.getGoodsSaleId());
                    if (goodsSale != null) {
                        goodsSale.setStatus(BusinessConstants.GOODS_1);
                        goodsSaleService.update(goodsSale);
                        iGoodsSaleMaterialService.deleteBySaleId(goodsSale.getId());
                    }
                    // 更新项目状态  考虑 单个订单  退回 和 多个订单合并退回的情况
                    for (String projectId : goodsSaledoItem.getProjectId().split(",")) {
                        ProjectBaseInfo order = projectBaseInfoService.selectById(projectId);
                        order.setStatus(BusinessConstants.PROJECT_1);
                        projectBaseInfoService.update(order);
                    }

                    // 返回库存里 锁定数量
                    GoodsOrderIn goodsOrderIn = goodsOrderInService.selectById(goodsSaledoItem.getGoodsId());
                    if (goodsOrderIn != null) {
                        goodsOrderIn.setRemainderCount(goodsOrderIn.getRemainderCount().add(goodsSale.getLockCount()));
                        goodsOrderInService.update(goodsOrderIn);
                    }
                }
            }
        }
        return toAjax(lvDoService.update(entity));
    }


    /**
     * 技术驳回
     */
    @PutMapping("/techReject")
    public AjaxResult techReject(@RequestBody LvDo entity) {
        // 先查询状态 请购单 是否是库管提交
        LvDo lvDo = lvDoService.selectById(entity.getId());
        /*if (!BusinessConstants.PLEASE_BUY_STATUS_STORE_SUBMIT.equals(lvDo.getStatus())) {
            return AjaxResult.error("该订单状态不是库管提交，请确认后重新弃审！");
        }*/
        entity.setStatus(BusinessConstants.PLEASE_BUY_STATUS_TECH_REJECT);
        GoodsSaledo goodsSaledo = new GoodsSaledo();
        goodsSaledo.setMainId(entity.getId());
        List<GoodsSaledo> goodsSaledoList = goodsSaledoService.selectList(goodsSaledo);

        if (BusinessConstants.PLEASE_BUY_SOURCE_ORDER_TWICE.equals(lvDo.getSource())) {
            // 如果是二次采购 不需要更新订单状态  不需要更新订单产品状态 返回锁定的库存
            for (GoodsSaledo goodsSaledoItem : goodsSaledoList) {
                // 返回库存里 锁定数量
                GoodsOrderIn goodsOrderIn = goodsOrderInService.selectById(goodsSaledoItem.getGoodsId());
                if (goodsOrderIn != null) {
                    goodsOrderIn.setRemainderCount(goodsOrderIn.getRemainderCount().add(goodsSaledoItem.getPleaseBuyCount()));
                    goodsOrderInService.update(goodsOrderIn);
                }
                GoodsSale goodsSale = goodsSaleService.selectById(goodsSaledoItem.getGoodsSaleId());
                if (goodsSale != null) {
                    goodsSale.setLockCount(goodsSale.getLockCount().add(goodsSaledoItem.getPleaseBuyCount()));
                    goodsSale.setPleaseBuyCount(BigDecimal.ZERO);
                    goodsSaleService.update(goodsSale);
                }
                goodsSaledoItem.setStatus(BusinessConstants.GOODS_32);
                goodsSaledoService.update(goodsSaledoItem);
            }

        } else {
            for (GoodsSaledo goodsSaledoItem : goodsSaledoList) {
                // 返回库存里 锁定数量
                GoodsOrderIn goodsOrderIn = goodsOrderInService.selectById(goodsSaledoItem.getGoodsId());
                if (goodsOrderIn != null) {
                    goodsOrderIn.setRemainderCount(goodsOrderIn.getRemainderCount().add(goodsSaledoItem.getPleaseBuyCount()));
                    goodsOrderInService.update(goodsOrderIn);
                }
                GoodsSale goodsSale = goodsSaleService.selectById(goodsSaledoItem.getGoodsSaleId());
                if (goodsSale != null) {
                    goodsSale.setStatus(BusinessConstants.GOODS_1);
                    goodsSaleService.update(goodsSale);
                    iGoodsSaleMaterialService.deleteBySaleId(goodsSale.getId());
                }
                // 更新项目状态  考虑 单个订单  退回 和 多个订单合并退回的情况
                if(StringUtils.isNotBlank(goodsSaledoItem.getProjectId())){
                    for (String projectId : goodsSaledoItem.getProjectId().split(",")) {
                        ProjectBaseInfo order = projectBaseInfoService.selectById(projectId);
                        order.setStatus(BusinessConstants.PROJECT_1);
                        projectBaseInfoService.update(order);
                    }
                }
                goodsSaledoItem.setStatus(BusinessConstants.GOODS_32);
                goodsSaledoService.update(goodsSaledoItem);
            }
        }
        // 更新请购单状态
        lvDoService.update(entity);
        processPleaseBuyService.insertPleaseBuyProcess(entity.getId(), "", "技术退回");
        return AjaxResult.success("操作成功！");
    }

    /**
     * 采购驳回
     */
    @PutMapping("/buyReject")
    public AjaxResult buyReject(@RequestBody LvDo entity) {
        entity.setStatus(BusinessConstants.PLEASE_BUY_STATUS_BUY_REJECT);
        // 先查询状态 请购单 是否是库管提交
        LvDo lvDo = lvDoService.selectById(entity.getId());
        if (!BusinessConstants.PLEASE_BUY_STATUS_TECH_SUBMIT.equals(lvDo.getStatus())) {
            return AjaxResult.error("该订单状态不是技术提交，请确认后重新弃审！");
        }
        goodsSaledoService.updateStatusByMainId(BusinessConstants.GOODS_33, entity.getId());
        GoodsSaledo goodsSaledo = new GoodsSaledo();
        goodsSaledo.setMainId(entity.getId());
        List<GoodsSaledo> goodsSaledoList = goodsSaledoService.selectList(goodsSaledo);

        for (GoodsSaledo goodsSaledoItem : goodsSaledoList) {
            GoodsSale goodsSale = goodsSaleService.selectById(goodsSaledoItem.getGoodsSaleId());
            if (goodsSale != null) {
                goodsSale.setStatus(BusinessConstants.GOODS_33);
                goodsSaleService.update(goodsSale);
                iGoodsSaleMaterialService.deleteBySaleId(goodsSale.getId());
            }
            goodsSaledoService.update(goodsSaledoItem);
        }
        // 插入日志
        processPleaseBuyService.insertPleaseBuyProcess(entity.getId(), "", "采购退回");
        return toAjax(lvDoService.update(entity));
    }

    /**
     * 采购同意的进行退回技术
     */
    @PutMapping("/buyRejectToJishu")
    public AjaxResult buyRejectToJishu(@RequestBody LvDo entity) {
        entity.setStatus(BusinessConstants.PLEASE_BUY_STATUS_BUY_REJECT);
        // 先查询状态 请购单 是否是库管提交
        LvDo lvDo = lvDoService.selectById(entity.getId());
        if (!BusinessConstants.PLEASE_BUY_STATUS_BUY_PASS.equals(lvDo.getStatus())) {
            return AjaxResult.error("该订单状态不是技术提交，请确认后重新弃审！");
        }
        goodsSaledoService.updateStatusByMainId(BusinessConstants.GOODS_33, entity.getId());
        GoodsSaledo goodsSaledo = new GoodsSaledo();
        goodsSaledo.setMainId(entity.getId());
        List<GoodsSaledo> goodsSaledoList = goodsSaledoService.selectList(goodsSaledo);

        for (GoodsSaledo goodsSaledoItem : goodsSaledoList) {
            GoodsSale goodsSale = goodsSaleService.selectById(goodsSaledoItem.getGoodsSaleId());
            if (goodsSale != null) {
                goodsSale.setStatus(BusinessConstants.GOODS_33);
                goodsSaleService.update(goodsSale);
                iGoodsSaleMaterialService.deleteBySaleId(goodsSale.getId());
            }
            goodsSaledoService.update(goodsSaledoItem);
        }
        return toAjax(lvDoService.update(entity));
    }


    //技术弃审  已废弃 2025-03-27
    @PutMapping("/editRejectKuguan")
    public AjaxResult editRejectCaigou(@RequestBody LvDo entity) {
        LvDo lvDo = lvDoService.selectById(entity.getId());
        if (!BusinessConstants.PLEASE_BUY_STATUS_STORE_SUBMIT.equals(lvDo.getStatus())) {
            return AjaxResult.error("技术部已提交，请联系后重新弃审！");
        }
        String changeStatus = entity.getStatus();
        if (StringUtils.isBlank(changeStatus)) {
            return AjaxResult.error("状态不能为空");
        }
        goodsSaledoService.updateStatusByMainId(BusinessConstants.GOODS_33, entity.getId());
//        if( BusinessConstants.PLEASE_BUY_SOURCE_MATERIAL.equals(lvDo.getSource())){
//            // 如果是材料采购 铁件记录
//            OrderIronRecord orderIronRecord  = orderIronRecordService.selectById(lvDo.getMainId());
//            orderIronRecord.setStatus(BusinessConstants.ORDER_IRON_STATE_REJECT);
//            orderIronRecordService.update(orderIronRecord);
//        }
        // 如果是库管驳回 更新订单产品状态
        if (BusinessConstants.PLEASE_BUY_STATUS_TECH_REJECT.equals(changeStatus)) {
            GoodsSaledo goodsSaledo = new GoodsSaledo();
            goodsSaledo.setMainId(entity.getId());
            List<GoodsSaledo> goodsSaledoList = goodsSaledoService.selectList(goodsSaledo);
            for (GoodsSaledo goodsSaledoItem : goodsSaledoList) {
                GoodsSale goodsSale = goodsSaleService.selectById(goodsSaledoItem.getGoodsSaleId());
                if (goodsSale != null) {
                    goodsSale.setStatus(BusinessConstants.GOODS_1);
                    goodsSaleService.update(goodsSale);
                    iGoodsSaleMaterialService.deleteBySaleId(goodsSale.getId());
                }
                ProjectBaseInfo order = projectBaseInfoService.selectById(goodsSaledoItem.getProjectId());
                order.setStatus(BusinessConstants.PROJECT_1);
                projectBaseInfoService.update(order);
                // 返回库存里 锁定数量
                GoodsOrderIn goodsOrderIn = goodsOrderInService.selectById(goodsSaledoItem.getGoodsId());
                if (goodsOrderIn != null) {
                    goodsOrderIn.setRemainderCount(goodsOrderIn.getRemainderCount().add(goodsSale.getLockCount()));
                    goodsOrderInService.update(goodsOrderIn);
                }
            }
        }
        return toAjax(lvDoService.update(entity));
    }

    /**
     * 删除请购单
     */
    @PreAuthorize("@ss.hasPermi('develop:LvDo:remove')")
    @Log(title = "请购单", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {
        return toAjax(lvDoService.deleteByIds(ids));
    }

    @Log(title = "其他请购提交请购单", businessType = BusinessType.DELETE)
    @GetMapping("/submitQgd/{ids}")
    public AjaxResult submitQgd(@PathVariable String[] ids) {
        if (StringUtils.isNotBlank(ids[0])) {
            LvDo lvDo = lvDoService.selectById(ids[0]);
            if (lvDo != null) {
                lvDo.setStatus(BusinessConstants.PLEASE_BUY_STATUS_TECH_SUBMIT);
                lvDoService.update(lvDo);
                goodsSaledoService.updateStatusByMainId(BusinessConstants.GOODS_3, lvDo.getId());
                return AjaxResult.success("提交成功！");
            } else {
                return AjaxResult.error("提交失败！");
            }
        } else {
            return AjaxResult.error("提交失败！");
        }
    }

    /**
     * 铁件 - 采购 - 驳回
     */
    @GetMapping("/ironBuyReject")
    public AjaxResult ironBuyReject(LvDo entity) {
        // 先查询状态 请购单 是否是库管提交
        LvDo lvDo = lvDoService.selectById(entity.getId());
        if (BusinessConstants.PLEASE_BUY_IRON_STATUS_BUY_REJECT.equals(lvDo.getIronStatus())) {
            return AjaxResult.error("该订单状态采购已经退回，请确认后重新弃审！");
        }
        entity.setIronStatus(BusinessConstants.PLEASE_BUY_IRON_STATUS_BUY_REJECT);
        GoodsSaledo goodsSaledo = new GoodsSaledo();
        goodsSaledo.setMainId(entity.getId());
        List<GoodsSaledo> goodsSaledoList = goodsSaledoService.selectList(goodsSaledo);

        for (GoodsSaledo goodsSaledoItem : goodsSaledoList) {
            goodsSaledoService.update(goodsSaledoItem);
            // 还库存可用数量
            GoodsOrderIn goodsOrderIn = goodsOrderInService.selectById(goodsSaledoItem.getGoodsId());
            goodsOrderIn.setRemainderCount(goodsOrderIn.getRemainderCount().add(goodsSaledoItem.getLockCount()));
            goodsOrderInService.update(goodsOrderIn);
        }
        return toAjax(lvDoService.update(entity));
    }

    /**
     * 铁件  采购  通过
     */
    @GetMapping("/ironBuyPass")
    public AjaxResult ironBuyPass(LvDo entity) {
        // 先查询状态 请购单 是否是库管提交
        LvDo lvDo = lvDoService.selectById(entity.getId());
        if (BusinessConstants.PLEASE_BUY_IRON_STATUS_BUY_PASS.equals(lvDo.getIronStatus())) {
            return AjaxResult.error("该订单状态采购已经通过，请确认后重新弃审！");
        }
        entity.setIronStatus(BusinessConstants.PLEASE_BUY_IRON_STATUS_BUY_PASS);
        entity.setStatus(BusinessConstants.PLEASE_BUY_STATUS_BUY_DEPT_PASS);
        GoodsSaledo goodsSaledo = new GoodsSaledo();
        goodsSaledo.setMainId(entity.getId());
        List<GoodsSaledo> goodsSaledoList = goodsSaledoService.selectList(goodsSaledo);
        for (GoodsSaledo goodsSaledoItem : goodsSaledoList) {
            goodsSaledoItem.setStatus(BusinessConstants.SALE_TYPE_13);
            goodsSaledoService.update(goodsSaledoItem);
        }
        return toAjax(lvDoService.update(entity));
    }

    /**
     * 铁件  库管  退回
     */
    @GetMapping("/storeReject")
    public AjaxResult storeReject(LvDo entity) {
        // 先查询状态 请购单 是否是库管提交
        LvDo lvDo = lvDoService.selectById(entity.getId());
        if (BusinessConstants.PLEASE_BUY_IRON_STATUS_STORE_REJECT.equals(lvDo.getIronStatus())) {
            return AjaxResult.error("该订单状态采购已经通过，请确认后重新弃审！");
        }
        entity.setIronStatus(BusinessConstants.PLEASE_BUY_IRON_STATUS_STORE_REJECT);
        entity.setStatus(BusinessConstants.PLEASE_BUY_STATUS_BUY_REJECT);

        GoodsSaledo goodsSaledo = new GoodsSaledo();
        goodsSaledo.setMainId(entity.getId());
        List<GoodsSaledo> goodsSaledoList = goodsSaledoService.selectList(goodsSaledo);
        for (GoodsSaledo goodsSaledoItem : goodsSaledoList) {
            goodsSaledoService.update(goodsSaledoItem);
        }
        return toAjax(lvDoService.update(entity));
    }






    /**
     * 请购单一键 转换成 采购单
     */
    @GetMapping(value = "/oneKeyToBuy/{id}")
    public AjaxResult oneKeyToBuy(@PathVariable("id") String id) {
        LvDo lvDo = lvDoService.selectById(id);
        lvDo.setGenBuy(BusinessConstants.STATUS_YES);
        lvDoService.update(lvDo);
        GoodsSaledo goodsSaledo = new GoodsSaledo();
        goodsSaledo.setMainId(id);
        List<GoodsSaledo> goodsSaledoList = goodsSaledoService.selectList(goodsSaledo);
        LvBuy lvBuy = new LvBuy();
        lvBuy.setLvDoId(id);
        lvBuy.setYwType(lvDo.getType());
        lvBuy.setSource(BusinessConstants.BUY_SOURCE_PLEASE);
        lvBuy.setGoodsType(BusinessConstants.GOODS_TYPE_HARDWARE);
        lvBuy.setFiles(lvDo.getFiles());
        lvBuyService.insert(lvBuy);
        for (GoodsSaledo saledo : goodsSaledoList) {
            GoodsSalebuy goodsSalebuy = new GoodsSalebuy();
            saledo.setBuyRemain(BigDecimal.ZERO);
            goodsSaledoService.update(saledo);
            GoodsSalebuyall goodsSalebuyall=goodsSalebuyallService.selectByGoodsIdAndRemark(saledo.getGoodsId(),saledo.getRemark(), SecurityUtils.getUsername());
            if(goodsSalebuyall!=null){
                goodsSalebuyall.setShengyu(saledo.getOrderCount().add(goodsSalebuyall.getShengyu()));
                goodsSalebuyall.setCounts(saledo.getOrderCount().add(goodsSalebuyall.getCounts()));
                goodsSalebuyall.setQinggouId(saledo.getId());
                goodsSalebuyall.setProjectName(saledo.getGongchengName());
                goodsSalebuyall.setGoodsUnitPrice(saledo.getGoodsUnitPrice());
                goodsSalebuyall.setMainId(lvBuy.getId());
                goodsSalebuyall.setGoodsType(saledo.getAddGoodsType());
                goodsSalebuyall.setCreateTime(DateUtils.getNowDate());
                goodsSalebuyall.setSaleDate(saledo.getSaleDate());
                goodsSalebuyall.setGoodsId(saledo.getGoodsId());
                goodsSalebuyall.setGoodsName(saledo.getGoodsName());
                goodsSalebuyall.setGoodsCode(saledo.getGoodsCode());
                goodsSalebuyall.setGoodModel(saledo.getGoodModel());
                goodsSalebuyall.setNos(saledo.getNos());
                goodsSalebuyall.setIsdo(saledo.getIsdo());
                goodsSalebuyallService.update(goodsSalebuyall);
            }else{
                goodsSalebuyall=new  GoodsSalebuyall();
                goodsSalebuyall.setId(IdUtils.fastSimpleUUID());
                goodsSalebuyall.setSaleDate(saledo.getSaleDate());
                goodsSalebuyall.setProjectName(saledo.getGongchengName());
                goodsSalebuyall.setQinggouId(saledo.getId());
                goodsSalebuyall.setMainId(lvBuy.getId());
                goodsSalebuyall.setStatus("5");
                goodsSalebuyall.setGoodsType(saledo.getAddGoodsType());
                goodsSalebuyall.setShengyu(saledo.getOrderCount());
                goodsSalebuyall.setCounts(saledo.getOrderCount());
                goodsSalebuyall.setGoodsId(saledo.getGoodsId());
                goodsSalebuyall.setGoodsName(saledo.getGoodsName());
                goodsSalebuyall.setGoodsCode(saledo.getGoodsCode());
                goodsSalebuyall.setGoodModel(saledo.getGoodModel());
                goodsSalebuyall.setNos(saledo.getNos());
                goodsSalebuyall.setIsdo(saledo.getIsdo());
                goodsSalebuyall.setCreateTime(DateUtils.getNowDate());
                goodsSalebuyall.setGoodsBuyTotal(saledo.getOrderCount());
                goodsSalebuyall.setGoodsTotalPrice(saledo.getOrderCount().multiply(saledo.getGoodsBuyPrice()));
                goodsSalebuyallService.insert(goodsSalebuyall);
            }
            GoodsSalebuy salebuy =new GoodsSalebuy();
            salebuy.setId(IdUtils.fastSimpleUUID());
            salebuy.setQinggouId(saledo.getId());
            salebuy.setMainId(lvBuy.getId());
            salebuy.setCaigouno(lvBuy.getNos());
            salebuy.setIsdo(saledo.getIsdo());
            salebuy.setCgallid(goodsSalebuyall.getId());
            salebuy.setStatus("5");
            salebuy.setShengyu(saledo.getOrderCount());
            salebuy.setSaleDate(saledo.getSaleDate());
            salebuy.setProjectName(saledo.getGongchengName());
            salebuy.setGoodsBuyPrice(saledo.getGoodsBuyPrice());
            salebuy.setGoodsUnitPrice(saledo.getGoodsUnitPrice());
            salebuy.setGoodsBuyTotal(saledo.getOrderCount().multiply(saledo.getGoodsBuyPrice()));
            salebuy.setNos(saledo.getNos());
            salebuy.setIsdo(saledo.getIsdo());
            salebuy.setGoodsName(saledo.getGoodsName());
            salebuy.setGoodsCode(saledo.getGoodsCode());
            salebuy.setGoodModel(saledo.getGoodModel());
            salebuy.setQinggouId(saledo.getId());
            goodsSalebuyService.insert(salebuy);
        }





        return AjaxResult.success(lvDoService.selectById(id));
    }













}
